#!/usr/bin/env python3
# -*- coding: utf-8 -*-
"""
启动脚本
"""

import requests
import json
import time

def test_mock_api():
    """测试Mock API接口"""
    base_url = "http://localhost:8084"
    
    print("开始测试Mock微信接口服务...")
    
    # 1. 测试健康检查
    print("\n1. 测试健康检查...")
    try:
        response = requests.get(f"{base_url}/health")
        print(f"健康检查响应: {response.json()}")
    except Exception as e:
        print(f"健康检查失败: {e}")
        return
    
    # 2. 获取访问令牌
    print("\n2. 获取访问令牌...")
    try:
        response = requests.get(f"{base_url}/cgi-bin/gettoken", params={
            "corpid": "wxc4c1ad1d26dc0daf",
            "corpsecret": "mock_secret_456"
        })
        token_data = response.json()
        print(f"获取令牌响应: {token_data}")
        
        if token_data.get("errcode") == 0:
            access_token = token_data.get("access_token")
        else:
            print("获取令牌失败")
            return
    except Exception as e:
        print(f"获取令牌失败: {e}")
        return
    
    # 3. 获取客服账号列表
    print("\n3. 获取客服账号列表...")
    try:
        response = requests.get(f"{base_url}/cgi-bin/kf/account/list", params={
            "access_token": access_token
        })
        print(f"客服账号列表响应: {response.json()}")
    except Exception as e:
        print(f"获取客服账号列表失败: {e}")
    
    # 4. 检查会话状态
    print("\n4. 检查会话状态...")
    try:
        response = requests.post(f"{base_url}/cgi-bin/kf/service_state/get", params={
            "access_token": access_token
        }, json={
            "external_userid": "mock_user_001",
            "open_kfid": "mock_kf_001"
        })
        print(f"会话状态响应: {response.json()}")
    except Exception as e:
        print(f"检查会话状态失败: {e}")
    
    # 5. 发送客服消息
    print("\n5. 发送客服消息...")
    try:
        response = requests.post(f"{base_url}/cgi-bin/kf/send_msg", params={
            "access_token": access_token
        }, json={
            "open_kfid": "mock_kf_001",
            "touser": "mock_user_001",
            "msgtype": "text",
            "text": {
                "content": "您好，我是客服助手，有什么可以帮助您的吗？"
            }
        })
        print(f"发送消息响应: {response.json()}")
    except Exception as e:
        print(f"发送消息失败: {e}")
    
    # 6. 获取客服消息
    print("\n6. 获取客服消息...")
    try:
        response = requests.post(f"{base_url}/cgi-bin/kf/sync_msg", params={
            "access_token": access_token
        }, json={
            "cursor": "",
            "limit": 10
        })
        print(f"获取消息响应: {response.json()}")
    except Exception as e:
        print(f"获取消息失败: {e}")
    
    # 7. 获取状态统计
    print("\n7. 获取状态统计...")
    try:
        response = requests.get(f"{base_url}/status")
        print(f"状态统计响应: {response.json()}")
    except Exception as e:
        print(f"获取状态统计失败: {e}")
    
    print("\n测试完成!")

if __name__ == "__main__":
    # 等待服务启动
    print("等待服务启动...")
    time.sleep(2)
    test_mock_api()
